package com.edoomath.api.web.controller.course;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;
import com.edoomath.api.servcice.CourseItemService;
import com.edoomath.api.servcice.CourseService;
import com.edoomath.api.servcice.UserCourseService;
import com.edoomath.api.servcice.UserService;
import com.edoomath.entity.course.Course;
import com.edoomath.entity.course.CourseItem;
import com.edoomath.entity.course.CourseMaterail;
import com.edoomath.entity.user.User;
import com.edoomath.entity.usercourse.UserCourse;

/**
 * 
 * @author mohanlan
 *
 */
@RestController
@RequestMapping("/courses")
public class CourseController {
	
	@Autowired
	private UserService userService;
    
    @Autowired
    private CourseService courseService;
    
    @Autowired
    private CourseItemService courseItemService;
    
    @Autowired
    private UserCourseService  userCourseService; 
    
    /**
     * 
     * @param id
     * @param opengid
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult loadCourse(@PathVariable(value = "id") Long id,
                                 @RequestParam(value = "opengid", required = false) String opengid) {
        
        HttpResult httpResult = new HttpResult();
        
        // load course
        Course course = courseService.load(Course.class, id);
        
        // list all course item
        List<CourseItem> items = courseItemService.listCourseItem(course);
        
        // list all course material by type
        Map<String, List<CourseMaterail>> materials = courseService.listMaterials(course);
        
        return httpResult.success()
                         .data("course", course)
                         .data("items", items)
                         .data("materials", materials);
    }

    
    /**
     * 
     * @param itemid
     * @param type
     * @param unionid
     * @return
     */
    @RequestMapping(value = "/{id}/materials", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult listMaterials(@PathVariable(value = "id") Long courseid,
    								@RequestParam(value = "type") String type,
    								@RequestParam(value = "unionid") String unionid) {
        
        HttpResult httpResult = new HttpResult();
        
        Course course = courseService.load(Course.class, courseid);
        
        // 1. course-item information
        List<CourseMaterail> materials = courseService.listCourseMaterial(course, type);
        
        CourseMaterail materialsZip = courseService.loadMaterialZip(course, com.demo2do.core.utils.StringUtils.connect(type, "_ZIP"));
        
        
        // 2. TODO get progress according to the user
        
        return httpResult.success().data("courseMaterails", materials)
        						   .data("courseMaterailsZip", materialsZip)
        						   .message("获取资料信息成功");
    }
    
    /**
     * 
     * @param courseId
     * @param unionid
     * @return
     */
    @RequestMapping(value = "/{id}/validate-user", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult validateUser(@PathVariable("id") Long courseId,
    							   @RequestParam("unionid") String unionid) {
    	
    	HttpResult httpResult = new HttpResult();
    	
    	User user = userService.load(unionid);
    	if(user.isChildren() && StringUtils.isNotBlank(user.getMobile())) {
    		user = userService.getParent(user);
    		unionid = user.getUnionid();
    	}
    	
    	UserCourse userCourse = userCourseService.loadUserCourse(unionid, courseId);
    	
    	if(userCourse == null) {
    		throw BusinessException.newInstance("400", "用户与课程未绑定");
    	}
    	
    	return httpResult.success()
    				     .data("userCourse", userCourse)
    					 .message("用户与课程已绑定");
    	
    }
}
